package com.adderis.soft.presentation.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.servlet.http.HttpSession;

import org.springframework.dao.DataAccessException;

import com.adderis.soft.domain.model.Users;
import com.adderis.soft.service.IUsersBo;

/**
 * 
 * User Managed Bean
 * 
 * @author reda
 * @version 1.0.0
 * 
 */
@ManagedBean(name = "userMB")
@SessionScoped
public class UserManagedBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final String SUCCESS = "success";
	private static final String ERROR = "error";

	// Spring User Service is injected...
	@ManagedProperty(value = "#{usersService}")
	IUsersBo userService;

	List<Users> userList;

	private int userID;
	private String logon;
	private String password;
	private String nom;
	private String prenom;
	private String role;
	private Users current;

	
	/**
	 *login
	 * @return String - Response Message 
	 */
	public String login(){
		
		try {
			Users user = new Users();
			user = getUserService().login(getLogon(), getPassword());
			if (user.getLogin() != null) {
				// get Http Session and store username
	            HttpSession session = Util.getSession();
	            session.setAttribute("name", logon);
	            return "success";
			}else {
				reset();
				return "login";
			}			
		} catch (DataAccessException e) {
			System.out.println("Error in login() -->" + e.getMessage());
            
		}
		
		return null;
	}
	
	 public String logout() {
	      HttpSession session = Util.getSession();
	      session.invalidate();
	      return "login";
	   }
	
	/**
	 * Add User
	 * 
	 * @return String - Response Message
	 */
	public String addUser() {
		try {
			Users user = new Users();
			user.setLogin(getLogon());
			user.setNom(getNom());
			user.setPrenom(getPrenom());
			user.setRole(getRole());
			getUserService().persist(user);
			return SUCCESS;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}

		return ERROR;
	}

	/**
	 * Reset Fields
	 * 
	 */
	public String  reset() {
		this.setUserID(0);
		this.setLogon("");
		this.setPassword("");
		this.setNom("");
		this.setPrenom("");
		this.setRole("");
		return null;
	}

	/**
	 * Get User List
	 * 
	 * @return List - User List
	 */
	public List<Users> getUserList() {
		userList = new ArrayList<Users>();
		userList.addAll(getUserService().findAll());
		return userList;
	}
	
	 public boolean isLoggedIn() {
	        return current != null;
	    }

	/**
	 * Get User Service
	 * 
	 * @return IUsersBo - User Service
	 */
	public IUsersBo getUserService() {
		return userService;
	}

	/**
	 * Set User Service
	 * 
	 * @param IUsersBo
	 *            - User Service
	 */
	public void setUserService(IUsersBo userService) {
		this.userService = userService;
	}

	/**
	 * Set User List
	 * 
	 * @param List
	 *            - User List
	 */
	public void setUserList(List<Users> userList) {
		this.userList = userList;
	}

	public int getUserID() {
		return userID;
	}

	public void setUserID(int userID) {
		this.userID = userID;
	}

	public String getLogon() {
		return logon;
	}

	public void setLogon(String login) {
		this.logon = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	/**
	 * Get User Id
	 * 
	 * @return int - User Id
	 */

}